home *** CD-ROM | disk | FTP | other *** search
/ Precision Software Appli…tions Silver Collection 1 / Precision Software Applications Silver Collection Volume One (PSM) (1993).iso / windows / games / wincapt.arj / HOOK.C < prev    next >
C/C++ Source or Header  |  1992-03-12  |  5KB  |  146 lines

  1. //***********************************************************************
  2. //
  3. // hook.c
  4. //
  5. // Source file for Keyboard Hook function and support procedures.  Defines
  6. // the following exported functions:
  7. //  
  8. // InstallHook()       - Installs/Removes keyboard hook for app's hotkeys
  9. // KeyboardHook()      - Keyboard Hook Procedure
  10. //
  11. // This file should go into a DLL.
  12. //
  13. // This file, although not officially containing DIB functions, is part 
  14. // of the DIBAPI.DLL because:
  15. //   1) System-wide Hook Procedures in 3.1 need to be in a DLL, and 
  16. //   2) I didn't want the user to have 2 DLLs to worry about for WINCAP. 
  17. //
  18. //
  19. // Development Team: Mark Bader
  20. //                   Tony Claflin
  21. //
  22. // Written by Microsoft Product Support Services, Developer Support.
  23. // Copyright (c) 1991 Microsoft Corporation. All rights reserved.
  24. //***********************************************************************
  25.  
  26. #include <windows.h>
  27. #include "wincap.h"
  28.  
  29. // Globals for this module
  30. static HWND ghWndMain;     // Handle to main window -- used to post msgs
  31. static HHOOK hHook;        // A handle to our installed hook
  32. static BOOL bHookInstalled = FALSE;  // TRUE if hook has been installed
  33.  
  34. // External variables
  35. extern HANDLE ghDLLInst;   // Handle to the DLL's instance.  Set in LibMain.
  36.  
  37. // Local function.  Although this function is exported from our DLL (Windows
  38. // needs to call it directly), no other app needs to call this, so we can
  39. // just prototype it here. 
  40.  
  41. LRESULT CALLBACK KeyboardHook (int nCode, WORD wParam, DWORD lParam );
  42.  
  43. //**********************************************************************
  44. // InstallHook()
  45. //
  46. // Installs/Removes Filter function for the WH_KEYBOARD hook.
  47. //
  48. // Parameters:
  49. // HWND hWnd      Handle to main window to receive posted messages.  See
  50. //                KeyboardHook() for more info on how it works.
  51. // 
  52. // BOOL bCode     TRUE to hook, FALSE to unhook
  53. //
  54. // Returns:
  55. // 1 if successful, 0 if not.
  56. //
  57. // History:   Date      Author         Reason
  58. //            03/04/92  Mark Bader     Created from HOOKS 3.1 SDK app
  59. //
  60. //**********************************************************************
  61.  
  62. int FAR PASCAL InstallHook (HWND hWnd, BOOL bCode )
  63. {
  64.  
  65.   int nReturn = 1;
  66.  
  67.   ghWndMain = hWnd;  // Store app's window handle in DLL global variable
  68.  
  69.   // Make sure that we are installing/removing in the proper order
  70.   if (bCode == bHookInstalled)
  71.     return (0);
  72.  
  73.   if (bCode) {
  74.       hHook = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardHook, ghDLLInst, NULL);
  75.       if (!hHook)
  76.         return(0);
  77.       bHookInstalled = TRUE;
  78.     }
  79.   else {
  80.       nReturn = UnhookWindowsHookEx(hHook);
  81.       bHookInstalled = FALSE;
  82.     }
  83.    return (nReturn);
  84. }
  85.  
  86.  
  87.  
  88.  
  89. //**********************************************************************
  90. //
  91. // KeyboardHook()
  92. //
  93. // This is the Keyboard Hook function which windows will call every
  94. // time it gets a keyboard message.  In this function, we check to
  95. // see if the key pressed was Ctrl+Alt+F[8,9,10], and if it is, we post
  96. // the proper message to our main window which will do the right
  97. // thing.  
  98. //
  99. // Note that the window handle that we post from was set by a call to
  100. // InstallHook() above.
  101. //
  102. // Parameters/return value:
  103. //
  104. // Standard 3.1 KeyboardProc.  See docs for "KeyboardProc".
  105. //
  106. // History:   Date      Author         Reason
  107. //            9/15/91   Tony Claflin   Created
  108. //            12/11/91  Mark Bader     Added additional hotkey Ctrl-Shift-F10
  109. //            03/04/92  Mark Bader     Moved into a DLL for Win 3.1
  110. //
  111. //*********************************************************************
  112.  
  113. LRESULT CALLBACK KeyboardHook (int nCode, WORD wParam, DWORD lParam )
  114. {
  115.  
  116.    if ( nCode >= 0 ) {
  117.  
  118.       // Check to see if it's a key we're looking for
  119.  
  120.       if (GetKeyState(VK_SHIFT) < 0 && GetKeyState(VK_CONTROL) < 0) {
  121.         switch (wParam) {
  122.  
  123. //      MENUITEM    "&Active Window \aCtrl+Shift+F9",  IDM_ACTIVEWINDOW
  124. //      MENUITEM    "&Desktop\aCtrl+Shift+F10",       IDM_DESKTOP
  125.  
  126.             case VK_F9:
  127.               if (HIWORD(lParam) & 0x8000)
  128.                  PostMessage(ghWndMain, WM_COMMAND, IDM_ACTIVEWINDOW, 0L);
  129.               return 1;
  130.               break;
  131.             case VK_F10:
  132.               if (HIWORD(lParam) & 0x8000)
  133.                  PostMessage(ghWndMain, WM_COMMAND, IDM_DESKTOP, 0L);
  134.               return 1;
  135.               break;
  136.             }
  137.         }
  138.    }
  139.  
  140.    //
  141.    // If we haven't returned 1 by the time we get here, then we
  142.    // need to pass on the message to CallNextHookEx.
  143.    //
  144.    return( (int)CallNextHookEx(hHook, nCode, wParam, lParam));
  145. }
  146.